<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>导出方法类</Title>
			<Shortcut>funcexport</Shortcut>
			<Description>导出方法定义类</Description>
			<Author>singba</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType> 
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
				<Literal>
					<ID>name</ID>
					<ToolTip>类型名称</ToolTip>
					<Default>FunctionClass</Default>
				</Literal>
				<Literal>
					<ID>namespace</ID>
					<ToolTip>命名空间</ToolTip>
					<Default>AF.Core</Default>
				</Literal>
				<Literal>
					<ID>type</ID>
					<ToolTip>返回值</ToolTip>
					<Default>object</Default>
				</Literal>
				<Literal>
					<ID>args</ID>
					<ToolTip>参数</ToolTip>
					<Default>object value</Default>
				</Literal>
			</Declarations>
			<Code Language="csharp">
<![CDATA[/*本文件遵循AF框架的方法导出标准*/
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using AF.Core;

namespace $namespace$
{
    public delegate $type$ $name$Delegate($args$);

    #region export attribute
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false), MetadataAttribute]
    public class $name$ExportAttribute : ExportAttribute, INamed
    {
        public const string ExportContractName = "Test$name$";

        public $name$ExportAttribute(string name)
            : base(ExportContractName,typeof($name$Delegate)) 
        { 
            Name = name;
        }

        public string Name { get; private set; }
    }
    #endregion

    #region 加载器
    public static class $name$s
    {
        #region 定义
        private static object _LoadLock = new object();
        private static Dictionary<string, $name$Delegate> _$name$s
            = new Dictionary<string, $name$Delegate>();

        static $name$s()
        {
            Load();
        }
        #endregion

        #region Load 加载所有的服务方法
        static void Load()
        {
            lock (_LoadLock)
            {
                _$name$s.Clear();
                var items = ServiceLocator.Current.GetContainer()
                    .GetExports<$name$Delegate, INamed>
                        ($name$ExportAttribute.ExportContractName);
                foreach (var item in items)
                {
                    _$name$s.Add(item.Metadata.Name, item.Value);
                }
            }
        }
        #endregion

        #region Run 
        // 根据名称运行函数 
        public static $type$ Run(string name,$args$)
        {
            $name$Delegate func = null;
            if (!_$name$s.TryGetValue(name, out func))
            {
                throw new Exception("没有对应的方法:" + name);
            }
            return func($args$$end$);
        }
        #endregion

        #region Register 注册方法
        /// <summary>
        /// 注册服务方法
        /// </summary>
        /// <param name="address">服务地址</param>
        /// <param name="func">方法执行函数</param>
        public static void Register(string address, $name$Delegate func)
        {
            lock (_LoadLock)
            {
                _$name$s[address] = func;
            }
        }
        #endregion

        #region 公用数据源
        [DataSource("$name$s")]
        public static IEnumerable<string> GetNames(params string[] paras)
        {
            if(paras!=null && paras.Length > 0)
            {
                yield return paras[0];
            }
            foreach(var key in _$name$s.Keys)
            {
                yield return key;
            }
        }
        #endregion

        #region 导出举例
        [$name$Export("Demo")]
        public static $type$ Demo($args$)
        {
            return null;
        }
        #endregion
    }
    #endregion
}]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>
